package com.qiwenshare.file.config.es;

import co.elastic.clients.elasticsearch.ElasticsearchClient;
import co.elastic.clients.json.jackson.JacksonJsonpMapper;
import co.elastic.clients.transport.ElasticsearchTransport;
import co.elastic.clients.transport.rest_client.RestClientTransport;
import org.apache.http.Header;
import org.apache.http.HttpHost;
import org.apache.http.message.BasicHeader;
import org.elasticsearch.client.RestClient;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.Collections;

@Configuration
public class ElasticSearchConfig {
    //原先配置
//    @Bean
//    public ElasticsearchClient elasticsearchClient(){
//        RestClient client = RestClient.builder(new HttpHost("localhost", 9200,"http")).build();
//        ElasticsearchTransport transport = new RestClientTransport(client,new JacksonJsonpMapper());
//        return new ElasticsearchClient(transport);
//    }

    @Bean
    public ElasticsearchClient elasticsearchClient() {
        RestClient client = RestClient.builder(new HttpHost("localhost", 9200, "http"))
                .setDefaultHeaders(new Header[]{
                        new BasicHeader("Content-Type", "application/json"),
                        new BasicHeader("X-Elastic-Product", "file")  // 用你的产品名称替代 "your-product-name"
                })
                .build();

        ElasticsearchTransport transport = new RestClientTransport(client, new JacksonJsonpMapper());
        return new ElasticsearchClient(transport);
    }
}